home *** CD-ROM | disk | FTP | other *** search
- Path: skivs.ski.org!usenet
- From: gt@ns.oon.or.jp (Gemini Thunder)
- Newsgroups: comp.lang.c
- Subject: problem with struct & qsort()???
- Date: Mon, 11 Mar 1996 16:07:54 GMT
- Organization: Smith-Kettlewell Eye Research Institute
- Message-ID: <4i1j7h$h8f@skivs.ski.org>
- NNTP-Posting-Host: oonserv.oon.or.jp
- X-Newsreader: Forte Free Agent 1.0.82
-
- Greetings and Salutations.
- I am stuck on a bug in a little program. I (think) have nailed it
- down, and am asking for your help. I will try to summarize the
- problem without posting pages of code.
-
- The purpose here is to take an array of structures, sort them,
- save them to disk, and later read them.
- So I use this struct STUDENT (containing struct DATE):
-
- /* DDMMMYY format */
- typedef struct d{
- int d_day;
- char d_month[4];
- int d_year;
- } DATE;
-
- typedef struct s{
- int std_num;
- char name[INPUT_SIZE + 1];
- DATE dob,
- *dptr;
- }STUDENT;
-
- so later i get an array:
- STUDENT recarray[MAX_STUDENTS]; /* maybe 20 or so, whatever */
-
- so i get the input, that is good, and i save to disk, that is good,
- and i read from disk and print out that is good.
- _BUT_ when i introduce a call to qsort() after i get all the structs,
- but before i save, the input is garbled.
-
- qsort( (void *)recarray, MAX_STUDENTS, sizeof(recarray[0]), sorter);
-
- with sorter() being:
-
- int sorter(const void *a, const void *b){
- return( ( ((STUDENT *)a) -> std_num) -
- ( ((STUDENT *)b) -> std_num) );
- }
-
- anyway, as i say if i just comment out the call to qsort() everything
- goes well. i have (out of desperation) tried replacing MAX_STUDENTS in
- the qsort() call with the actual number of structs in array , to no
- avail.
-
- also a breakpoint in a "debug" version of sorter() (using variables)
- reveals that the b -> std_num is incorrect, but a -> std_num seems
- okay.
-
- i have ran the code through Clint, nothing turned up. there are no
- errors nor warnings when i compile, with all options turned on (turbo
- C++ 3.0 / msdos)
-
- i can find nothing in any book or online documentation i have on a
- special problem using structs with qsort() so i am forced to conclude
- i am an idiot.
-
- any help would be greatly appreciated, or i can just post the entire
- source if i did not give enough information here. thank you.
-
-